package com.sgzn.sgpay.util;

import com.sgzn.sgpay.enums.ReturnResultState;
import com.sgzn.sgpay.exception.SgpayException;
import org.apache.http.util.TextUtils;

import java.util.Collection;

/**
 * 校验工具类
 * @author huangsenguang
 * @version Id: ArgsUtils.java, v 0.1 2018/6/4 22:38 huangsenguang Exp $$
 */
public class ArgsUtils {
    private ArgsUtils() {
    }

    /**
     * expression 结果为fasle 抛出SgpayException异常
     * @param expression
     * @param code
     * @param desc
     */
    public static void check(boolean expression, String code, String desc) {
        if(!expression) {
            throw new SgpayException(code, desc);
        }
    }

    public static void check(boolean expression, String message, Object... args) {
        if(!expression) {
            throw new IllegalArgumentException(String.format(message, args));
        }
    }

    public static void check(boolean expression, String message, Object arg) {
        if(!expression) {
            throw new IllegalArgumentException(String.format(message, new Object[]{arg}));
        }
    }

    public static <T> T notNull(T argument, String code, String desc) {
        if(argument == null) {
            throw new SgpayException(code, desc);
        } else {
            return argument;
        }
    }

    public static <T extends CharSequence> T notEmpty(T argument, String code, String desc) {
        if(argument == null) {
            throw new SgpayException(code, desc);
        } else if(TextUtils.isEmpty(argument)) {
            throw new SgpayException(code, desc);
        } else {
            return argument;
        }
    }

    /**
     * 非空判断
     * @param argument
     * @param code
     * @param desc
     * @param <T>
     * @return
     */
    public static <T extends CharSequence> T notBlank(T argument, String code, String desc) {
        if(argument == null) {
            throw new SgpayException(code, desc);
        } else if(TextUtils.isBlank(argument)) {
            throw new SgpayException(code, desc);
        } else {
            return argument;
        }
    }

    public static <T extends CharSequence> T containsNoBlanks(T argument, String code, String desc) {
        if(argument == null) {
            throw new SgpayException(code, desc);
        } else if(TextUtils.containsBlanks(argument)) {
            throw new SgpayException(code, desc);
        } else {
            return argument;
        }
    }

    /**
     * 集合非空
     * @param argument
     * @param code
     * @param desc
     * @param <E>
     * @param <T>
     * @return
     */
    public static <E, T extends Collection<E>> T notEmpty(T argument, String code, String desc) {
        if(argument == null) {
            throw new SgpayException(code, desc);
        } else if(argument.isEmpty()) {
            throw new SgpayException(code, desc);
        } else {
            return argument;
        }
    }

    /**
     * 集合为空
     * @param argument
     * @param code
     * @param desc
     * @param <E>
     * @param <T>
     * @return
     */
    public static <E, T extends Collection<E>> T isEmpty(T argument, String code, String desc) {
        if(argument == null) {
            return argument;
        } else if(argument.isEmpty()) {
            return argument;
        } else {
            throw new SgpayException(code, desc);
        }
    }

    /**
     * n <= 0 抛出SgpayException异常
     * @param n
     * @param code
     * @param desc
     * @return
     */
    public static int positive(int n, String code, String desc) {
        if(n <= 0) {
            throw new SgpayException(code, desc);
        } else {
            return n;
        }
    }

    /**
     * n <= 0 抛出SgpayException异常
     * @param n
     * @param code
     * @param desc
     * @return
     */
    public static long positive(long n, String code, String desc) {
        if(n <= 0L) {
            throw new SgpayException(code, desc);
        } else {
            return n;
        }
    }


    /**
     * n>0 抛出SgpayException异常
     * @param n
     * @param code
     * @param desc
     * @return
     */
    public static int notPositive(int n, String code, String desc) {
        if(n > 0) {
            throw new SgpayException(code, desc);
        } else {
            return n;
        }
    }

    /**
     * n<0 抛出SgpayException异常
     * @param n
     * @param code
     * @param desc
     * @return
     */
    public static int notNegative(int n, String code, String desc) {
        if(n < 0) {
            throw new SgpayException(code, desc);
        } else {
            return n;
        }
    }

    /**
     * n<0 抛出SgpayException异常
     * @param n
     * @param code
     * @param desc
     * @return
     */
    public static long notNegative(long n, String code, String desc) {
        if(n < 0L) {
            throw new SgpayException(code, desc);
        } else {
            return n;
        }
    }

    public static String verifyResult(int n) {
        if (n >= 1) {
            return ReturnResultState.SUCCESS.getCode();
        } else {
            return ReturnResultState.FAIL.getCode();
        }
    }
}

